﻿using BCT.Models;
using BCT.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCT.Domains
{
    public class OccuManageDomain : IOccuManageDomain
    {
        private readonly IOccuManageRepository _occuManageRepository;

        public OccuManageDomain(OccuManageRepository occuManageRepository)
        {
            _occuManageRepository = occuManageRepository;
        }
        public List<OccupationModel> GetOccupationList(RoleAndOccupationModel model) {
            return _occuManageRepository.GetOccupationList(model);
        }
        //获取occupation的下拉列表
        public List<ResOccuListModel> GetOccupationSelect() {
            var occupationSelect = _occuManageRepository.GetOccupationSelect();
            var result = occupationSelect
                        .Select(s => new ResOccuListModel()
                        {
                            value = s.OccuId,
                            name = s.OccuName
                        }).ToList();
            return result;
        }
        //修改role时获取occupation下拉列表并选中部分occupation
        public List<ResOccuListModel> GetOccuListByEditRole(RoleAndOccupationModel model)
        {
            var occupationlistbyid = _occuManageRepository.GetOccupationList(model);
            var occupationlistselect = _occuManageRepository.GetOccupationSelect();
            var result = (from ot in occupationlistselect
                          join od in occupationlistbyid
                          on ot.OccuId equals od.OccuId into odt
                          from s in odt.DefaultIfEmpty()    //左连接
                          select new ResOccuListModel()
                          {
                              value = ot.OccuId,
                              name = ot.OccuName,
                              selected = s == null ? "" : "selected"
                          }).ToList();
            return result;
        }
    }
}
